function KTHdata = loadKTHData( rootdir, posefolder, flowfolder )
%loadKTHData - Loads pose and flow data.

% Examples:
%   rootdir = '/Users/trananh/Desktop/gestures/KTH/ground-truth';
  
  if ~strcmp(rootdir(end), '/')
    rootdir = [rootdir '/'];
  end
  
  clc;
  
  maxactors = 25;       % hard coded number of actors
  
  % Initialize some structures
  poseheaders = [];
  actionsdata = [];
  
  % Iterate over each action in the rootdir
  actions = getSubDirectories(rootdir);
  for i = 1:numel(actions)
    [~, actionname, ~] = fileparts(actions{i});
    actiondir = [actions{i} '/'];
    persondata = cell(1,maxactors);
    
    % Iterate over each clip for the action
    clips = getSubDirectories(actiondir);
    for j = 1:numel(clips)
      [~, clipname, ~] = fileparts(clips{j});
      idx = regexp(clipname, '\d{2}', 'once');
      pid = str2num(clipname(idx:idx+1));
      assert (pid >= 1 && pid <= maxactors), 'Expect 1 <= pid <= 25!';
      clipdir = [clips{j} '/'];
      segdata = [];
      
      % Iterate over each segment for the clip
      segments = getSubDirectories(clipdir);
      for k = 1:numel(segments)
        [~, segmentid, ~] = fileparts(segments{k});
        segmentdir = [segments{k} '/'];
        
        % Make sure it's a valid segment
        if ~isempty(regexp(segmentdir, '.*\d{5}-\d{5}/$', 'once'))
          
          % Load pose
          posedir = [segmentdir posefolder '/'];
          fileslist = dir([posedir '*.txt']);
          if numel(fileslist) > 0
            assert (numel(fileslist) == 1), 'Expect only 1 actor!';
            posedat = importdata([posedir fileslist(1).name]);
            pose = posedat.data;
            if isempty(poseheaders)
              poseheaders = posedat.colheaders;
            end
          else
            fprintf('** Missing pose for dir: %s\n', posedir);
            pose = [];
          end
          
          % Load flow
          flowdir = [segmentdir flowfolder '/'];
          fileslist = dir([flowdir '*.txt']);
          if numel(fileslist) > 0
            assert (numel(fileslist) == 1), 'Expect only 1 actor!';
            file = [flowdir fileslist(1).name];
            s = dir(file);
            if s.bytes > 0
              flow = dlmread(file);
            else
              fprintf('** Empty flow for dir: %s\n', flowdir);
              flow = [];
            end
          else
            fprintf('** Missing flow for dir: %s\n', flowdir);
            flow = [];
          end
          
          % Create data segment
          seg = struct('segmentid', segmentid, 'pose', pose, ...
            'flow', flow);
          segdata = cat(2, segdata, seg);
        end
      end
      
      % Create a datapoint for the video clip
      segdata = struct('clipname', clipname, 'segments', segdata);
      
      % Add clip to the action for the appropriate actor
      persondata{pid} = cat(2, persondata{pid}, segdata);
    end
    
    % Store data for action
    datapoint = struct('actionname', actionname, 'persons', {persondata});
    actionsdata = cat(2, actionsdata, datapoint);
    
  end
  
  % Create the final pose structure
  KTHdata = struct('rootdir', rootdir, 'actions', actionsdata, ...
    'poseheaders', {poseheaders});
  
end

